<template>
  <div class="box">
    <!-- 搜索框 -->
    <div class="ipt">
      <el-input
        @change="changeIpt"
        v-model="input"
        class="input"
        placeholder="请输入内容"
      ></el-input>
      <el-button type="primary">查询</el-button>
      <el-button type="primary" @click="addshop">添加</el-button>
    </div>
    <!-- 表单 -->
    <div class="form">
      <template>
        <el-table :data="tableData" border style="width: 100%">
          <el-table-column type="selection" width="55"></el-table-column>
          <el-table-column prop="id" label="商品ID" width="125">
          </el-table-column>
          <el-table-column
            prop="title"
            label="商品名称"
            show-overflow-tooltip
            width="125"
          >
          </el-table-column>
          <el-table-column prop="price" label="商品价格" width="125">
          </el-table-column>
          <el-table-column prop="num" label="商品数量" width="125">
          </el-table-column>
          <el-table-column prop="category" label="商品类目" width="125">
          </el-table-column>
          <el-table-column prop="image" label="商品图片" width="205">
          </el-table-column>
          <el-table-column
            prop="sellPoint"
            label="商品卖点"
            show-overflow-tooltip
            width="205"
          >
          </el-table-column>
          <el-table-column prop="descs" label="商品描述" width="205">
          </el-table-column>
          <el-table-column label="操作" width="210">
            <template slot-scope="scope">
              <el-button
                size="mini"
                @click="handleEdit(scope.$index, scope.row)"
                >编辑</el-button
              >
              <el-button
                size="mini"
                type="danger"
                @click="handleDelete(scope.$index, scope.row)"
                >删除</el-button
              >
            </template>
          </el-table-column>
        </el-table>
      </template>
    </div>

    <!-- 分页器 -->
    <Pagniton
      :total="total"
      :pageSize="pageSize"
      @changePage="changePage"
      :currentPage="currentPage"
    ></Pagniton>
    <!-- 弹框 -->
    <Dialog
      :dialogVisible="dialogVisible"
      @changeDialog="changeDialog"
      ref="Dialog"
      :title="title"
      :rowData="rowData"
    ></Dialog>
  </div>
</template>

<script>
import Pagniton from "../../components/Pagniton.vue";
import Dialog from "./Dialog.vue";
export default {
  data() {
    return {
      input: "",
      tableData: [],
      total: 10,
      pageSize: 1,
      list: [],
      type: 1,
      dialogVisible: false,
      currentPage: 1,
      title: "添加商品",
      rowData: {},
    };
  },
  components: {
    Pagniton,
    Dialog,
  },
  methods: {
    changeDialog() {
      this.$refs.Dialog.log = false;
    },
    addshop() {
      this.$refs.Dialog.log = true;
      this.title = "添加商品";
    },
    //编辑
    handleEdit(index, row) {
      this.$refs.Dialog.log = true;
      this.title = "编辑商品";
      this.rowData = {...row};
    },
    //删除
    handleDelete(index, row) {
      this.$confirm("此操作将永久删除该商品, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.$api
            .deleteGoods({
              id: row.id,
            })
            .then((res) => {
              console.log(res.data);
              if (res.data.status === 200) {
                this.$message({
                  type: "success",
                  message: "删除成功!",
                });
                this.http(1);
              }
            });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    changePage(num) {
      this.currentPage = num;
      if (this.type == 1) {
        this.http(num);
      } else {
        this.tableData = this.list.slice((num - 1) * 3, num * 3);
      }
    },
    changeIpt(val) {
      if (!val) {
        this.http(1);
        this.currentPage = 1;
        this.type = 1;
        return;
      }
      console.log(val);
      this.$api
        .getSearch({
          search: val,
        })
        .then((res) => {
          console.log("---------搜索", res.data);
          this.currentPage = 1;
          if (res.data.status == 200) {
            this.list = res.data.result;
            this.total = res.data.result.length;
            this.pageSize = 3;
            this.tableData = res.data.result.slice(0, 3);
            this.type = 2;
          } else {
            this.tableData = [];
            this.total = 1;
            this.pageSize = 1;
            this.type = 1;
          }
        });
    },
    http(page) {
      this.$api
        .getGoodsList({
          page,
        })
        .then((res) => {
          console.log(res.data);
          if (res.data.status === 200) {
            this.tableData = res.data.data; //数据列表
            this.total = res.data.total;
            this.pageSize = res.data.pageSize;
          }
        });
    },
  },
  created() {
    this.http(1);
  },
};
</script>

<style lang="less" scoped>
.box {
  background: #eee;
  .ipt {
    display: flex;
    padding: 20px;
    .input {
      margin-right: 15px;
    }
  }
  .form {
    padding: 20px;
    padding-top: 0;
  }
}
</style>